package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/eduservice/video")   //调用服务的名称
//@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    //注入vodClient
    @Autowired
    private VodClient vodClient;

    //添加章节
     @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
         videoService.save(eduVideo);
      return   R.ok();
     }

    //删除小节，删除对应阿里云视频
    //TODO 这个方法需要完善
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
         EduVideo eduVideo=videoService.getById(id);
         String videoSourceId=eduVideo.getVideoSourceId();

         //判断小节里面是否有视频id
         if(!StringUtils.isEmpty(videoSourceId)){
             //根据视频id，远程调用实现视频删除
             vodClient.removeAlyVideo(videoSourceId);
         }
         //删除小节
         videoService.removeById(id);
         return R.ok();
    }
    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
       return R.ok();
    }

    //根据小节id查询
    @GetMapping("getVideoInfo/{id}")
    public R getVideoInfo(@PathVariable String id){
        EduVideo eduvideo=videoService.getById(id);
        return R.ok().data("video",eduvideo);
    }

}

